package com.ming.closeto.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ming.closeto.R;
import com.ming.closeto.bean.MusicListBean;
import com.ming.closeto.utils.GlideUtils;

import java.util.List;

public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.ViewHolder>{

    private Context mContext;
    private LayoutInflater inflater;
    private List<MusicListBean> mList;

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @SuppressLint("ServiceCast")
    public MusicListAdapter(Context mContext, List<MusicListBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.layout_music_list_item, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHoleder, final int position) {
        MusicListBean bean = mList.get(position);
        GlideUtils.loadImageviewCenterCrop(mContext, bean.getUrl(), viewHoleder.iv_img);
        viewHoleder.tv_text.setText(bean.getTitle());

        viewHoleder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    // ViewHolder 需要继承
    public class ViewHolder extends RecyclerView.ViewHolder{

        private ImageView iv_img;
        private TextView tv_text;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_img = itemView.findViewById(R.id.iv_img);
            tv_text = itemView.findViewById(R.id.tv_text);
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }
}